<?php
namespace app\logic;

use app\models\AdminAuthor;
/**
* 
*/
class AuthorLogic
{

	public function getAuthorList()
    {
        $author = AdminAuthor::find()->asArray()->All();
        $list = [];
        if (!empty($author)) {
            foreach ($author as $v) {
                if (empty($v['menu2']) && empty($v['menu3'])) {
                     $list[$v['menu']]['auth'] = $v;
                } elseif (!empty($v['menu2']) && empty($v['menu3'])) {
                     $list[$v['menu']][$v['menu2']]['auth'] = $v;
                } elseif (!empty($v['menu2']) && !empty($v['menu3'])) {
                     $list[$v['menu']][$v['menu2']][$v['menu3']]['auth'] = $v;
                }
            }
        }
        return $list;
    }

	public function getAdminAuthorInfo($id)
    {
        $result = AdminAuthor::find()->where(['id'=>$id])->asArray()->one();
        return $result;
    }

	public function saveAdminAuthorInfo($data)
    {
        $author=null;
        if($data && $data['id'] && !empty($data['id'])) $author = AdminAuthor::find()->where(['id'=>$data['id']])->one();
        if($author){
            $author->menu   = $data['menu'];
            $author->menu2  = $data['menu2'];
            $author->menu3  = $data['menu3'];
            $author->action = $data['action'];
            $author->update_date = date('Y-m-d H:i:s');
            $result = $author->save();
        }else{
            $author = new AdminAuthor();
            $author->menu   = $data['menu'];
            $author->menu2  = $data['menu2'];
            $author->menu3  = $data['menu3'];
            $author->action = $data['action'];
            $author->create_date = date('Y-m-d H:i:s');
            $author->update_date = date('Y-m-d H:i:s');
            $result = $author->save();
        }
        return $result;
    }

    public function deleteAuthor($id)
    {
        $result = false;
        $author = AdminAuthor::find()->where(['id'=>$id])->one();
        if($author){
             $result = $author->delete();
        }
        return $result;
    }

    public function getAuthorTree() {
        $rows = AdminAuthor::find()->all();
        $data = array();
        //遍历一级菜单
        foreach( $rows as $key => $row ) {
            if ( !empty( $row[ 'menu' ] ) && empty( $row[ 'menu2' ] ) && empty( $row[ 'menu3' ] ) ) {
                $data[ $row[ 'menu' ] ] = array( 'id' => $row[ 'id' ], 'name' => $row[ 'menu' ], 'sub' => array() );
                unset( $rows[ $key ] );
            }
        }
        //遍历二级菜单
        foreach( $rows as $key => $row ) {
            if ( !empty( $row[ 'menu' ] ) && !empty( $row[ 'menu2' ] ) && empty( $row[ 'menu3' ] ) ) {
                $tmp = isset($data[ $row[ 'menu' ] ])?$data[ $row[ 'menu' ] ]:'';
                if ( !empty( $tmp ) ) {
                   $data[ $row[ 'menu' ] ][ 'sub' ][ $row[ 'menu2' ] ] = array( 'id' => $row[ 'id' ], 'name' => $row[ 'menu2' ],  'sub' => array() );
                    unset( $rows[ $key ] );
                }
            }
        }
        //遍历三级菜单
        foreach( $rows as $key => $row ) {
            if ( !empty( $row[ 'menu' ] ) && !empty( $row[ 'menu2' ] ) && !empty( $row[ 'menu3' ] ) ) {
               $tmp = isset($data[ $row[ 'menu' ] ][ 'sub' ][ $row[ 'menu2' ] ]) ? $data[ $row[ 'menu' ] ][ 'sub' ][ $row[ 'menu2' ] ]:'';
               if ( !empty( $tmp ) ) {
                   $data[ $row[ 'menu' ] ][ 'sub' ][ $row[ 'menu2' ] ][ 'sub' ][ $row[ 'menu3' ] ] = array( 'id' => $row[ 'id' ], 'name' => $row[ 'menu3' ] );
                   unset( $rows[ $key ] );
               }
            }
        }
        return $data;
    }
}
